define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/sale/index' + location.search,
                    add_url: 'user/sale/add',
                    edit_url: 'user/sale/edit',
                    del_url: 'user/sale/del',
                    multi_url: 'user/sale/multi',
                    import_url: 'user/sale/import',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'acct_id', title: __('平台'), searchList: Config.adminlist, formatter: Table.api.formatter.normal },
                        { field: 'pool', title: __('Username'), visible: false },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'avatar', title: __('Avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'allocas.channel', title: __('已分配渠道'), searchList: Config.userlist, visible: false, formatter: Table.api.formatter.normal },
                        { field: 'allocas.assign', title: __('认领人'), searchList: Config.userlist, visible: false, formatter: Table.api.formatter.normal },
                        { field: 'allocas.is_claim', title: __('是否认领'), visible: false, searchList: { "0": "未认领", "1": "已认领" }, formatter: Table.api.formatter.normal },
                        // { field: 'parent_id', title: __('Parent_id') },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });
            // $(document).on("click", ".btn-download", function () {
            //     layer.confirm(`是否确定下载？`, {
            //         btn: ['确定', '取消'] //按钮
            //     }, function (index) {
            //         location.href = '/template/sale.xlsx'
            //         layer.close(index);
            //     }
            //     );
            // });
            $('#distributionButton').on('click', function (row) {
                var ids = Table.api.selectedids(table);
                Fast.api.open('user/sale/allocation?ids=' + ids.join(","), '批量分配');
            });
            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var data = $(this).data("value")

                if (data === 1) {
                    $("#distributionButton").addClass("hidden");
                    $(".btn-add").addClass("hidden");
                    table.bootstrapTable('showColumn', 'allocas.channel')
                    table.bootstrapTable('showColumn', 'allocas.assign')
                    table.bootstrapTable('showColumn', 'allocas.is_claim')
                    table.bootstrapTable('hideColumn', 'operate')
                } else {
                    $(".btn-add").removeClass("hidden");
                    $("#distributionButton").removeClass("hidden");
                    table.bootstrapTable('hideColumn', 'allocas.channel')
                    table.bootstrapTable('hideColumn', 'allocas.assign')
                    table.bootstrapTable('hideColumn', 'allocas.is_claim')
                    table.bootstrapTable('showColumn', 'operate')

                }
                table.bootstrapTable('refresh', {});
                return false;
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        allocation: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
